package ui.activity.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.StringUtil;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.ScrollViewFinal;
import event.NetErrorEvent;
import ui.activity.mine.biz.MyWalletBiz;
import ui.activity.mine.component.DaggerMyWalletComponent;
import ui.activity.mine.contract.MyWalletContract;
import ui.activity.mine.module.MyWalletModule;
import ui.activity.mine.presenter.MyWalletPresenter;
import ui.model.AccountMoneyInfo;
import ui.model.WithDrawInfo;


/**
 * 我的钱包
 * Created by chenjian on 2017/8/4.
 */

public class MyWalletAct extends BaseAct implements MyWalletContract.View {


    @Inject
    MyWalletPresenter presenter;
    @Inject
    MyWalletBiz biz;

    @BindView(R.id.money1)
    TextView money1;
    @BindView(R.id.money2)
    TextView money2;
    @BindView(R.id.money3)
    TextView money3;
    @BindView(R.id.money4)
    TextView money4;
    @BindView(R.id.money5)
    TextView money5;

    @BindView(R.id.my_wallet_bank)
    TextView bankCard;
    @BindView(R.id.my_wallet_fen_integral)
    TextView fenIntegral;
    @BindView(R.id.sv)
    ScrollViewFinal sv;
    @BindView(R.id.ptr_layout)
    PtrClassicFrameLayout ptr;
    @BindView(R.id.draw1)
    TextView draw1;
    @BindView(R.id.draw2)
    TextView draw2;
    @BindView(R.id.draw3)
    TextView draw3;
    @BindView(R.id.draw4)
    TextView draw4;
    @BindView(R.id.draw5)
    TextView draw5;
    @BindView(R.id.my_wallet_rewards_check)
    TextView check;





    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_mywallet);
    }

    @Override
    protected void setup() {
        DaggerMyWalletComponent.builder().myWalletModule(new MyWalletModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.mywallet), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getUserIncome(true);
            }
        });
        RxView.clicks(bankCard)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toCardList());
        RxView.clicks(draw1)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toReturNofincome("1"));
        RxView.clicks(draw2)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toReturNofincome("2"));
        RxView.clicks(draw3)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toReturNofincome("10"));
        RxView.clicks(draw4)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toReturNofincome("11"));
        RxView.clicks(draw5)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toReturNofincome("12"));
        RxView.clicks(fenIntegral)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toScore());
        RxView.clicks(check)
                .compose(this.bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toRewards());
        ptr.autoRefresh();
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getUserIncome(false);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Override
    public void upDateUI(AccountMoneyInfo info) {
        try {
            money1.setText(StringUtil.getFormatPrice(info.getDividedTotal()));
            money2.setText(StringUtil.getFormatPrice(info.getReturnTotal()));
            money3.setText(StringUtil.getFormatPrice(info.getTx()));
            money4.setText(StringUtil.getFormatPrice(info.getCz()));
            money5.setText(StringUtil.getFormatPrice(info.getLd()));
        } catch (Exception e) {

        }
    }

    @Override
    public void loadMoreComplete() {
        sv.onLoadMoreComplete();
    }


    @Override
    public void hasLoadMore(boolean b) {
        sv.setHasLoadMore(false);
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void upDateInfo(WithDrawInfo info) {
    }


}
